package geektime.spring.springbucks;

import geektime.spring.springbucks.model.Coffee;
import geektime.spring.springbucks.service.CoffeeService;
import geektime.spring.springbucks.service.CoffeeServiceImpl;
import io.lettuce.core.ReadFrom;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.redis.LettuceClientConfigurationBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Slf4j
@SpringBootApplication
@MapperScan("geektime.spring.springbucks.mapper")
public class SpringBucksApplication implements ApplicationRunner {

	@Autowired
	private CoffeeService coffeeService;

	public static void main(String[] args) {
		SpringApplication.run(SpringBucksApplication.class, args);
	}

	@Bean
	public RedisTemplate<String, Coffee> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
		RedisTemplate<String, Coffee> template = new RedisTemplate<>();
		template.setConnectionFactory(redisConnectionFactory);
		return template;
	}

	@Bean
	public LettuceClientConfigurationBuilderCustomizer customizer() {
		return builder -> builder.readFrom(ReadFrom.MASTER_PREFERRED);
	}

	@Override
	public void run(ApplicationArguments args) throws Exception {
		//添加数据
		addCoffee();
		findCoffeeByPage(1,0);

		//删除数据
		delCoffeeByName("test");
		findCoffeeByPage(1,0);

		//修改数据
		updateCoffeeByID("updateData",5000,4L);
		findCoffeeByPage(1,0);

		log.info("分页查询开始");
		//分页查询
		findCoffeeByPage(1,3);
		//根据主键批量查询
		findCoffeeByListID();

		findCoffeeFromRedis("addTest");

	}



	/**
	 * 分页查询数据
	 * @param page
	 * @param size
	 */
	private void findCoffeeByPage(int page, int size){
		List<Coffee> coffee = coffeeService.findAllCoffeeByPage(page,size);
		coffee.forEach(c -> log.info("Page(1) Coffee {}", c));
	}

	/**
	 * 根据主键批量查询，id：主键
	 */
	public void findCoffeeByListID() {

		List<Long> idList = new ArrayList();
		idList.add(1L);
		idList.add(3L);
		idList.add(5L);
		List<Coffee> coffee = coffeeService.findCoffeeByListID(idList);
		coffee.forEach(c -> log.info("根据主键批量查询 Coffee {}", c));
	}

	/**
	 * 分页查询数据
	 * @param name
	 */
	private void findCoffeeFromRedis(String name){
		Optional<Coffee> c = coffeeService.findOneCoffee(name);
		log.info("Value from Redis: {}", c);

		coffeeService.findOneCoffee(name);
	}

	/**
	 * 添加数据
	 */
	private void addCoffee(){
		log.info("添加操作开始，当前数据库条数: {}", getCount());
		coffeeService.addCoffee("addTest", 8000);
		log.info("添加操作后数据库条数: {}", getCount());
	}

	/**
	 * 删除数据
	 * @param name
	 */
	private void delCoffeeByName(String name){
		log.info("删除操作开始，当前数据库条数: {}", getCount());
		coffeeService.delCoffeeByName(name);
		log.info("删除操作后数据库条数: {}", getCount());
	}

	/**
	 * 修改数据
	 * @param id
	 */
	private void updateCoffeeByID(String name, long price, long id) {
		log.info("修改删除操作开始，当前数据库条数: {}", getCount());
		coffeeService.updateCoffeeByID(name,price,id);
		log.info("修改操作后数据库条数: {}", getCount());
	}


	private long getCount() {
		long count= coffeeService.getCount();
		return count;
	}
}

